<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Goods;
use App\Qingwuit\Models\InviteCode;
use App\Qingwuit\Models\Invites;
use App\Qingwuit\Models\Retails;
use App\Qingwuit\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsBrandsController extends Controller
{
    protected $modelName = 'GoodsBrand';
    public function stores(){
        $goodsModel = $this->getService('Store', true);
        $city_id = \request()->city_id;
        $type = \request()->type;
        $sort = \request()->sort;
        $userId = $this->getUserId('users');
        // 关键词
        if (isset($city_id) && !empty($city_id)) {
            $city_id = urldecode($city_id);
            $goodsModel = $goodsModel->where('city_id', 'like', '%' . $city_id . '%');
        }
        //查询商户
        $address = \request()->input('address');
        //查询商户
        $baseQuery = $goodsModel->where('store_status', 1);

        switch ($type) {
            case 1:
                $store = $baseQuery
                    ->orderBy('create_at', $sort)
                    ->select('id', 'store_logo', 'store_name', 'created_at')
                    ->get();
                break;
            case 2:
                $store = $baseQuery
                    ->orderBy('service_evaluation', $sort)
                    ->select('id', 'store_logo', 'store_name', 'created_at')
                    ->get();
                break;
            case 3:
                $store = $baseQuery
                    ->leftJoin('goods', 'goods.store_id', '=', 'stores.id')
                    ->select('stores.id', 'stores.store_logo', 'stores.store_name', 'stores.created_at', DB::raw('SUM(goods.goods_sale) as total_sales'))
                    ->groupBy('stores.id')
                    ->orderBy('total_sales', $sort)
                    ->get();
                break;
            default:
                // 如果没有匹配的$type，可以设置一个默认值或抛出异常
                $store = $baseQuery
                    ->select('id', 'store_logo', 'store_name', 'created_at')
                    ->get(); // 或者你可以根据需要处理这个情况
                break;
        }

        //查询每个商户下的商品
        foreach ($store as $k => $v) {
            $binding = Retails::where('distribution_id', $v->id)->where('shop_id', $userId)->first();
            $data = Goods::where('store_id', $v->id)->where('goods_status', 1)->where('is_recommend', 1)->get();
            if ($data->count() == 0) {
                $data = Goods::where('store_id', $v->id)->where('goods_status', 1)->inRandomOrder()->take(10)->get();
            }
            if (!$data->isEmpty()) {
                if (is_null($binding)) {
                    foreach ($data as $key => $value) {
                        $value->goods_price = "***";
                    }
                } else {
                    $biliId = InviteCode::where('code', $binding->code)->first();
                    $bili = Invites::where('id', $biliId->pid)->first();
                    foreach ($data as $key => $value) {
                        $value->goods_price = round($value->goods_price + $value->goods_price * $bili->percentage / 100, 2);;
                    }
                }
                $v['chinren'] = $data;
            }
        }

        foreach ($store as $k => $item) {
            if (!isset($item->chinren)){
                unset($store[$k]);
            }
        }
        return $this->format($store);
    }
    public function  search(Request $request)
    {
        $goodsModel = $this->getService('Goods', true);
            $user_id = $this->getUserId('users');
        $keywords = $request->keywords;

        // 关键词
        if (isset($keywords) && !empty($keywords)) {
            $keywords = urldecode($keywords);
            $goodsModel = $goodsModel->where('goods_name', 'like', '%' . $keywords . '%');
        }
        $data = $goodsModel->get();

        foreach ($data as $key => $val){
            $val->store_name = Store::where('id',$val->store_id)->first()->store_name;
            $binding = Retails::where('distribution_id', $val->id)->where('shop_id', $user_id)->first();
            if (!$binding){
                $val->goods_price = "***";
            }
        }
        return $this->format($data);

    }
}
